package per.dg.basicusethread.threadstate

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.util.Log
import per.dg.basicusethread.R

class ThreadStateActivity : AppCompatActivity() {

    private val TAG = "ThreadStateActivity"

    val handler : Handler = object : Handler() {
        override fun handleMessage(msg: Message) {
            super.handleMessage(msg)
            when(msg.what){
                0 ->{
                    Log.e(TAG, "handleMessage:  ${thread.state}" )
                }
                1 ->{
                    Log.e(TAG, "handleMessage:  调用方法lock.wait()后状态-${thread.state}" )
                    myNotify()
                }
                2 ->{
                    Log.e(TAG, "handleMessage:  调用方法thread.join(1000)后状态-${thread.state}" )
                }
                3 ->{
                    Log.e(TAG, "handleMessage:  调用方法thread2.join()后状态-${thread.state}" )
                }
                4 ->{
                    Log.e(TAG, "handleMessage:  调用方法Thread.sleep(1000)后状态-${thread.state}" )
                }
                5 ->{
                    TODO()
                    Log.e(TAG, "handleMessage:  调用方法Thread.yield()后状态-${thread.state}（因为是抢夺 cpu 使用权，所以不一定是 RUNNABLE 或者 WAITING,暂时未实现）" )
                }
                6 ->{
                    thread4 = Thread(object : Runnable {
                        override fun run() {
                            while (thread4_1){
                                printNumber(100)
                            }
                        }
                    })
                    thread4.start()
                    Thread.sleep(1000)

                    Log.e(TAG, "handleMessage:  thread4 访问已经被使用的锁对象， 状态为-${thread4.state}" )
                    thread4_1 = false
                }
            }
        }
    }

    val runnable = object : Runnable {
        override fun run() {
            for(i in 1..10){
                printNumber(i)
            }

        }
    }

    lateinit var thread : Thread
    lateinit var thread2 : Thread
    lateinit var thread3 : Thread
    lateinit var thread4 : Thread

    var thread4_1 = true

    private val lock = Object()

//    @Synchronized
//    private fun printNumber(int: Int){
//        Log.e(TAG, "printNumber: $int" )
//        while(int >= 9){
//            lock.wait()
//        }
//        if(int == 10){
//            handler.sendEmptyMessageDelayed(0, 10000)
//        }
//    }

    fun printNumber(int: Int) = synchronized(lock){
        Log.e(TAG, "printNumber: $int" )

        var intclone = int


//        while(intclone == 5){
//            handler.sendEmptyMessageDelayed(1, 1000)
//            intclone = intclone + 1
//            lock.wait()
//        }
        if(intclone == 4){
            handler.sendEmptyMessageDelayed(6, 100)
            Thread.sleep(1000*10)
        }

        if(intclone == 5){
            handler.sendEmptyMessageDelayed(1, 1000)
//            等待 lock 锁唤醒后继续执行
            lock.wait()
        }

        if(intclone == 6){
            handler.sendEmptyMessageDelayed(4, 100)
//            线程睡眠 1000 ms 后继续执行
            Thread.sleep(1000)
        }

        if(intclone == 7){
//            等待 1000 ms 后继续执行
//            handler.sendEmptyMessageDelayed(2, 100)
//            thread.join(1000)


            var i = 0
            while (i == 0){
                if(thread3.state == Thread.State.NEW){
                    thread3.start()
                }
                while (thread3.state == Thread.State.RUNNABLE){
                    i = 1
                    handler.sendEmptyMessageDelayed(2, 100)
//                    等待 1000 ms 后，重新抢夺 cpu
                    thread3.join(1000)
                    break
                }
            }
        }

        if(intclone == 8){
            handler.sendEmptyMessageDelayed(5,1)
//            让出 cpu，重新抢夺 cpu 使用权
            Thread.yield()
        }

        if(intclone == 9){
            var i = 0
            while (i == 0){
                if(thread2.state == Thread.State.NEW){
                    thread2.start()
                }
                while (thread2.state == Thread.State.RUNNABLE){
                    i = 1
                    handler.sendEmptyMessageDelayed(3, 1000)
//                    等待指定线程执行完成后再执行当前线程
                    thread2.join()
                }
            }
        }


//        线程执行完成后，查看线程状态
        if(intclone == 10){
            handler.sendEmptyMessageDelayed(0, 1000)
        }
    }

    fun myNotify() = synchronized(lock){
        lock.notify()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_thread_state)

        thread = Thread(runnable)
        Log.e(TAG, "run: ${thread.state}" )
        thread.start()
        Log.e(TAG, "run: ${thread.state}" )

        thread2 = Thread(object : Runnable {
            override fun run() {
                var i = System.currentTimeMillis()
                while (System.currentTimeMillis() - i < 1000*10){

                }
            }
        })

        thread3 = Thread(object : Runnable {
            override fun run() {
                var i = System.currentTimeMillis()
                while (System.currentTimeMillis() - i < 1000*10){
//                    Log.e(TAG, "run: System.currentTimeMillis() - i")
                }
            }
        })
    }
}